﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Dycox.Reporting
{
    public class TableCell : TablePart
    {
        public TableCell()
        {

        }

        private TableRow _row;

        public TableRow Row
        {
            get { return _row; }
            internal set
            {
                if (_row != value)
                {
                    NotifyTableLayoutChanged();
                    _row = value;
                    NotifyTableLayoutChanged();
                }
            }
        }

        public override Table Table
        {
            get
            {
                if (_row != null)
                    return _row.Table;
                else
                    return null;
            }
        }

        private void NotifyTableLayoutChanged()
        {
            Table table = Table;
            if (table != null)
                table.NotifyLayoutChanged(this);
        }

        private int _rowSpan = 1;

        [XmlAttribute("rowSpan")]
        public int RowSpan
        {
            get { return _rowSpan; }
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException();

                if (_rowSpan != value)
                {
                    _rowSpan = value;
                    NotifyTableLayoutChanged();
                }
            }
        }

        private int _colSpan = 1;
        [XmlAttribute("colSpan")]
        public int ColSpan
        {
            get { return _colSpan; }
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException();

                if (_colSpan != value)
                {
                    _colSpan = value;
                    NotifyTableLayoutChanged();
                }
            }
        }

        private string _text;

        [XmlAttribute("text")]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        


        private bool _disableEval;

        public bool DisableEval
        {
            get { return _disableEval; }
            set { _disableEval = value; }
        }


        /// <summary>
        /// 获取当前单元格所在<see cref="TableCellCollection"/>集合的索引。
        /// </summary>
        public int CellIndex
        {
            get
            {
                if (_row != null)
                {
                    return _row.Cells.IndexOf(this);
                }
                else
                    return -1;
            }
        }

        public TableColumn Column
        {
            get
            {
                if (PhysicalCell != null)
                    return Table.Columns[PhysicalCell.ColumnIndex];
                else
                    return null;
            }
        }

        protected override IHasStyle[] GetStyleParents()
        {
            if (Table != null)
                return new IHasStyle[] { Row, Column };
            else
                return null;
        }

        //internal List<PhysicalCell> OccupiedCells;

        //public int ColumnIndex
        //{
        //    get
        //    {
        //        if (OccupiedCells != null && OccupiedCells.Count > 0)
        //        {
        //            PhysicalCell pc = OccupiedCells[0];
        //            System.Diagnostics.Debug.Assert(pc.Occupation == CellOccupation.Normal);
        //            return pc.ColumnIndex;
        //        }
        //        else
        //            return -1;
        //    }
        //}

        internal PhysicalCell PhysicalCell { get; set; }
    }
}
